/**
 * 搜索节流指令
 * 默认 500ms
 * <el-input v-search-throttle="handleSearch"></el-input>
 */
import type { Directive, App } from 'vue';

const searchDirective: Directive = {
  mounted(el, binding) {
    let fn = binding.value
    let timer:any, timer_end:any;
    el.addEventListener('input', () => {
      if (timer) {
        clearTimeout(timer_end);
        return timer_end = setTimeout(() => fn(), 500);
      }
      fn();
      timer = setTimeout(() => timer = null, 500)
    })
  }
}

export function setSearchDirective(app: App) {
  app.directive('search-throttle', searchDirective);
}